<?php
/**
 *
 *
 */
class Yz_Model_Field_Timestamp
    extends Yz_Model_Field_Abstract
{
    const DEFAULTVALUE = YZ_TIME;



    /** Initialization value
     *  @return
     */
    public function assignValue($value)
    {
        if (null === $value)
            return $this->getDefaultValue();
        $value = (int)$value;
        return $value ?: null; // 0 counts as null
    }


    /** Test if a value is a valid number
     *  @param mixed value
     *  @return int>=0|null
     *  @throw TypeError
     *  @throw ValueError
     */
    protected function _testValue($value)
    {
        if (! is_numeric($value))
            throw TypeError::_new_();
        $value = (int)$value;
        if ($value < 0) // negative value is a type error
            throw TypeError::_new_();

        if (0 === $value)
            $value = null;
        return $value;
    }

}
